package org.ezplatform.office.rd.sfdj.web.rest;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.ApiOperation;
import org.ezplatform.core.web.view.OperationResult;
import org.ezplatform.sdk.AccessToken;
import org.ezplatform.sdk.Client;
import org.ezplatform.util.JsonUtils;
import org.ezplatform.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 值班管理
 * @author
 * @date 2023/06/30
 */
@RestController
@RequestMapping(value = "/api/w/rd/sfdj")
public class SfdjController {
    private static final Logger logger = LoggerFactory.getLogger(SfdjController.class);

    @Resource
    private JdbcTemplate jdbcTemplate;


    @ApiOperation(value = "移动端三方对接得根据手机号获取用户登录名得方法", notes = "移动端三方对接得根据手机号获取用户登录名得方法")
    @RequestMapping(value = {"/getLoginNameByMobile"}, method = {RequestMethod.POST})
    public OperationResult getLoginNameByMobile(String mobile) {
        //System.out.println("=============gaoyw====mobile:"+mobile);
        String sql = "select login_name from sys_user where telephone='"+mobile+"'  ";
        //System.out.println("=============gaoyw====sql:"+sql);
        List list = jdbcTemplate.queryForList(sql);
        return OperationResult.buildSuccessResult(list);
    }

    @ApiOperation(value = "移动端三方对接得根据手机号获取用户登录名得方法", notes = "移动端三方对接得根据手机号获取用户登录名得方法")
    @RequestMapping(value = {"/getLoginNameByMobileProcess"}, method = {RequestMethod.POST})
    public OperationResult getLoginNameByMobileProcess(String mobile,String processcode) throws Exception {
        //logger.error(">>>>>>>>>>>mobilemobilemobile" + mobile);
        //logger.error(">>>>>>>>>>>processcodeprocesscodeprocesscode" + processcode);
        String sql = "select ID,login_name,org_id from sys_user where telephone='"+mobile+"'  ";
        List<Map<String, Object>> rlist=jdbcTemplate.queryForList(sql,new Object[]{});
        String login_name ="";
        String org_id ="";
        String ID ="";
        if(rlist.size()>0){
            for(int a=0;a<rlist.size();a++) {
                Map amap = (Map) rlist.get(a);
                ID= amap.get("ID") + "";
                 login_name = amap.get("login_name") + "";
                 org_id= amap.get("org_id") + "";
            }
        }


        String BASE_URL = "http://172.24.240.124:18088/flexbase";
        String openTaskId = "";
        String processInstanceId = "";
        Client client = new Client();
        String url = BASE_URL + "/api/bpm/runtime/startProcess";
        System.out.println("====gaoyw====启动流程url: " + url);
        String corpId = "0";
        String code = processcode;//流程编码
        String groupId = org_id;
        String userId = ID;
        Map bodyParams = new HashMap();
        Map params = new HashMap();
        params.put("userId", userId);
        params.put("corpId", corpId);
        params.put("groupId", groupId);
        params.put("code", code);
        ObjectMapper mapper = new ObjectMapper();
        String bodyDataParams = mapper.writeValueAsString(bodyParams);
        String json = client.post(url, params, bodyDataParams, getAccess_token());
        Map result = JsonUtils.readValue(json);
        if (result.get("type").toString().equals("success")) {
            Map data = (Map)result.get("data");
            System.out.println(data);
            openTaskId = StringUtils.null2String(data.get("openTaskId"));
            processInstanceId=StringUtils.null2String(data.get("processInstanceId"));
        }

        List list=new ArrayList();
        list.add(login_name);
        list.add(openTaskId);
        list.add(processInstanceId);
        //logger.error(">>>>>>>>>>>processInstanceIdprocessInstanceId" + processInstanceId);
        //logger.error(">>>>>>>>>>>login_namelogin_name" + login_name);
        //logger.error(">>>>>>>>>>>processInstanceId" + processInstanceId);
        return OperationResult.buildSuccessResult(list);
    }




    //获取临时token
    public static AccessToken getAccess_token()  throws Exception {
        String url="http://172.24.240.124:18088/flexbase/oauth2/token";
        Client client = new Client();
        String client_secret="CwSgxC3alRiODHUTeAxC";//
        String client_id="OwlcVZp96gbfQnjyOGH0";//
        Map<String, String> params = new HashMap<String, String>();
        params.put("client_id", client_id);
        params.put("client_secret", client_secret);
        params.put("grant_type", "client_credentials");
        AccessToken acctessToken = client.getToken(url, params);
        //System.out.println("====JiangChao====acctessToken: " + acctessToken.getAccessToken());
        return acctessToken;
    }

    //获取用户token
    public static  String   ssoAuth2(String leaderLoginName)  throws Exception {
        String url="http://172.24.240.124:18088/flexbase/api/system/logon/ssoAuth2";
        Client client = new Client();
        String token="";
        Map<String, String> params = new HashMap<String, String>();
        AccessToken acctessToken=getAccess_token();
        params.put("token ", acctessToken.getAccessToken());
        params.put("corpId", "0");
        params.put("loginType", "1");
        params.put("uid", leaderLoginName);
        String json = client.post(url, params, "", acctessToken);
        Map result = JsonUtils.readValue(json);
        if (result.get("type").toString().equals("success")) {
            Map data = (Map)result.get("data");
            token = StringUtils.null2String(data.get("token"));
            //System.out.println("====JiangChao====token: " + token);
        }
        return token;
    }




}
